package com.atguigu.distributed.lock.service;

import com.atguigu.distributed.lock.mapper.StockMapper;
import com.atguigu.distributed.lock.pojo.Stock;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.ReentrantLock;

@Service
public class StockService {
//    private Stock stock = new Stock();

    @Autowired
    private StockMapper stockMapper;

    private ReentrantLock lock = new ReentrantLock();

    public void deduct() {
//        lock.lock();
        try {
            Stock stock = this.stockMapper.selectById(new QueryWrapper<Stock>().eq("product_code", "1001"));
            if(stock!=null&&stock.getCount()>0){
                stock.setCount(stock.getCount()-1);
                this.stockMapper.updateById(stock);
            }
//            this.stock.setStock(stock.getStock() - 1);
        } finally {
//            lock.unlock();
        }
//        System.out.println("库存余量：" + stock.getStock());
    }
}
